package com.jijuxie.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jijuxie.system.domain.SysRole;
import com.jijuxie.system.domain.SysRoleMenu;
import com.jijuxie.system.domain.SysUserRole;
import com.jijuxie.system.mapper.SysRoleMapper;
import com.jijuxie.system.mapper.SysRoleMenuMapper;
import com.jijuxie.system.mapper.SysUserRoleMapper;
import com.jijuxie.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public List<String> selectRolesByUserId(Long userId) {
        return baseMapper.selectRolesByUserId(userId);
    }

    @Override
    public SysRole selectRoleById(Long roleId) {
        return baseMapper.selectRoleById(roleId);
    }

    @Override
    public boolean checkRoleNameUnique(String roleName) {
        return count(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleName, roleName)) == 0;
    }

    @Override
    public boolean checkRoleKeyUnique(String roleKey) {
        return count(new LambdaQueryWrapper<SysRole>()
                .eq(SysRole::getRoleKey, roleKey)) == 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleMenus(Long roleId, List<Long> menuIds) {
        // 删除原有权限
        roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId));

        // 添加新权限
        if (!menuIds.isEmpty()) {
            List<SysRoleMenu> list = menuIds.stream().map(menuId -> {
                SysRoleMenu rm = new SysRoleMenu();
                rm.setRoleId(roleId);
                rm.setMenuId(menuId);
                return rm;
            }).collect(Collectors.toList());
            roleMenuMapper.insertBatch(list);
        }
    }

    @Override
    public List<Long> selectMenuIdsByRoleId(Long roleId) {
        return roleMenuMapper.selectMenuIdsByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAuthUsers(Long roleId, List<Long> userIds) {
        List<SysUserRole> list = userIds.stream().map(userId -> {
            SysUserRole ur = new SysUserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            return ur;
        }).collect(Collectors.toList());
        userRoleMapper.insertBatch(list);
    }

    @Override
    public void deleteAuthUser(Long roleId, Long userId) {
        userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getRoleId, roleId)
                .eq(SysUserRole::getUserId, userId));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        // 删除角色与菜单关联
        roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, id));
        // 删除角色与用户关联
        userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getRoleId, id));
        // 删除角色
        return super.removeById(id);
    }
}
